package com.lizemin.monitor.client.endpoint;

import com.lizemin.monitor.client.interceptor.ApiCounter;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lzm
 * @date 2025/6/9 20:36
 * @description 自定义端点
 * api调用次数统计，端点对象需要注册到spring容器中，才能被actuator扫描到
 */
@Component
@Endpoint(id = "apiCounter")
public class ApiStaticEndPoint {

    /**
     * 查看所有接口的调用次数
     *
     * @return 接口的调用次数
     */
    @ReadOperation
    public Map<String, Integer> apiCall() {
        return ApiCounter.getApiCounters().entrySet().stream()
                .collect(Collectors.toMap(Map.Entry::getKey,
                        e -> e.getValue().get()));
    }

    /**
     * 重置统计数据
     */
    @WriteOperation
    public void resetCounter() {
        ApiCounter.getApiCounters().clear();
    }

}
